import React from 'react';
import {Select} from 'antd'

const Option = Select.Option;
export default {
    formateDate(time) {
        if (!time) return '';
        let date = new Date(time);
        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },
    pagination(data, callback) {
        return {
            onChange: (current) => {
                callback(current);
            },
            current: data.result.page,
            pageSize: data.result.page_size,
            total: data.result.total_count,
            showTotal: () => {
                return `共${data.result.total_count}条`
            },
            showQuickJumper: true
        }
    },
    getOptionList(data) {
        if (!data) {
            return [];
        } else {
            let options = [];
            data.map((item) => {
                options.push(<Option value={item.code} key={item.id}>{item.value}</Option>)
            });
            return options;
        }
    },
    updateSelectedItem(selectedRowKeys, selectedItem, selectedIds) {//封装Table 点击当前行 把当前行 key存起来，显示选中
        if (selectedIds) {//多选
            this.setState({
                selectedRowKeys, selectedItem, selectedIds
            })
        } else {
            this.setState({
                selectedRowKeys, selectedItem
            })
        }
    },
    getMqttUrl() {
        // return   '120.92.169.67';
        return '127.0.0.1'
    },
    changeTwoDecimal_f(x) {
        var f_x = parseFloat(x);
        if (isNaN(f_x)) {
            alert('function:changeTwoDecimal->parameter error');
            return false;
        }
        var f_x = Math.round(x * 100) / 100;
        var s_x = f_x.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0) {
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2) {
            s_x += '0';
        }
        return s_x;
    }

}